package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.service.SupplierService;
import org.apache.commons.beanutils.ConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SupplierServiceImpl implements SupplierService {
    @Autowired
    private com.atguigu.jxc.dao.SupplierDao supplierDao;

    @Override
    public Map<String, Object> getSupplierList(Integer page, Integer rows, String supplierName) {
        List<Supplier> supplierList = supplierDao.getSupplierList((page-1)*rows, rows, supplierName);
        HashMap<String, Object> map = new HashMap<>();
        map.put("total", supplierList.size());
        map.put("rows",supplierList);
        return map;
    }

    @Override
    public ServiceVO SavaSupplier(Supplier supplier) {

        Supplier supplierDb = supplierDao.getSupplierById(supplier.getSupplierId());
        if (supplierDb != null) {
            //有数据更新
            supplierDao.updateSupplier(supplier.getSupplierId(), supplier.getSupplierName(),supplier.getContacts(),supplier.getPhoneNumber(),supplier.getAddress(),supplier.getRemarks());
        }else {
            //无数据保存
            supplierDao.saveSupplier(supplier.getSupplierName(),supplier.getContacts(),supplier.getPhoneNumber(),supplier.getAddress(),supplier.getRemarks());
        }


        return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS,null);
    }

    @Override
    public void deleteById(String ids) {
        String[] split = ids.split(",");
        List<String> list = Arrays.asList(split);
        supplierDao.deleteById(list);
    }
}
